package gamedemo;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import gamedemo.camera.*;
import gamedemo.component.animation.AnimationActions;
import gamedemo.component.animation.AnimationMapComponent;
import gamedemo.component.attack.AttackComponent;
import gamedemo.component.movement.PlayerMovementComponent;
import gamedemo.component.render.AnimationRenderComponent;
import gamedemo.component.render.BackgroundRenderComponent;
import gamedemo.entity.Entity;
import gamedemo.geom.Vector3f;
import gamedemo.manager.EntityManager;

import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Demo extends BasicGame {

	ArrayList<Entity> backgroundList;
	EntityManager entityManager;
	Entity monkey;
	Entity roads;
	Entity hills;
	Entity clouds;

	Camera cam;

	Demo() {
		super("Monkey Vengeance");
	}

	@Override
	public void init(GameContainer gc) throws SlickException {
		entityManager = new EntityManager();
		cam = new Camera(gc);

		backgroundList = new ArrayList<Entity>();
		backgroundList.add(new Entity("clouds", entityManager));
		backgroundList.get(backgroundList.size() - 1).addComponent(new BackgroundRenderComponent(new Image("/Images/clouds.png"), cam, 4));
		backgroundList.add(new Entity("Hills", entityManager));
		backgroundList.get(backgroundList.size() - 1).addComponent(new BackgroundRenderComponent(new Image("/Images/hills.png"), cam, 3));
		backgroundList.add(new Entity("Roads", entityManager));
		backgroundList.get(backgroundList.size() - 1).addComponent(new BackgroundRenderComponent(new Image("/Images/roads.png"), cam, 1));

		Image[] leftAnimation = { new Image("/Images/monkey_left.png") };
		Image[] leftJumpAnimation = { new Image("/Images/monkey_left_jump.png") };
		Image[] leftWalkingAnimation = { new Image("/Images/monkey_left.png"), new Image("/Images/monkey_left2.png"), new Image("/Images/monkey_left.png"), new Image("/Images/monkey_left3.png") };
		Image[] rightAnimation = { new Image("/Images/monkey_right.png") };
		Image[] rightWalkingAnimation = { new Image("/Images/monkey_right.png"), new Image("/Images/monkey_right2.png"), new Image("/Images/monkey_right.png"), new Image("/Images/monkey_right3.png") };
		Image[] rightJumpAnimation = { new Image("/Images/monkey_right_jump.png") };

		// LinkedHashMap : "sorterad" HashMap som kommer ih�g
		// ins�ttningsordningen n�r man itererar genom keySet() eller values()
		LinkedHashMap<AnimationActions, Animation> monkeyAnimations = new LinkedHashMap<AnimationActions, Animation>(6);
		monkeyAnimations.put(AnimationActions.JUMP_RIGHT, new Animation(rightJumpAnimation, 1000));
		monkeyAnimations.put(AnimationActions.JUMP_LEFT, new Animation(leftJumpAnimation, 1000));
		monkeyAnimations.put(AnimationActions.WALK_RIGHT, new Animation(rightWalkingAnimation, 100));
		monkeyAnimations.put(AnimationActions.WALK_LEFT, new Animation(leftWalkingAnimation, 100));
		monkeyAnimations.put(AnimationActions.IDLE_RIGHT, new Animation(rightAnimation, 100));
		monkeyAnimations.put(AnimationActions.IDLE_LEFT, new Animation(leftAnimation, 1000));

		monkey = new Entity("Monkey", entityManager);
		
		monkey.addComponent(new PlayerMovementComponent());
		monkey.addComponent(new AnimationRenderComponent(new Animation(rightAnimation, 100)));
		monkey.addComponent(new AnimationMapComponent(monkeyAnimations));
		monkey.addComponent(new AttackComponent());
	
		monkey.initComponents();

		monkey.setPosition(new Vector3f(461, 300, 0));
		
		entityManager.add(monkey);
		
		gc.setTargetFrameRate(60);
	}

	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		entityManager.update(gc, null, delta);

		for (Entity e : backgroundList) {
			e.update(gc, null, delta);
		}
		cam.update(monkey, gc, delta);
	}

	@Override
	public void render(GameContainer gc, Graphics gr) throws SlickException {
		cam.render(gr);
		for (Entity e : backgroundList) {
			e.render(gc, null, gr);
		}

		entityManager.render(gc, null, gr);
		
		cam.renderDone(gr);
	}

	public static void main(String[] args) throws SlickException {
		AppGameContainer app = new AppGameContainer(new Demo());

		app.setDisplayMode(1020, 540, false);
		app.start();
	}

}
